/**
 * 加载所有jquery插件
 */
define([
    "jquery",
    "cookie",
    "browser",
    "time",
    "url",
    "md5",
    "percent",
    "fileName",
    "imgErrorHandler",
    "photoviewer",
    "scaleImage"
], function ($) {
    /**
     * 给数字填充0，常用于月日时分秒一位数填充为两位
     * @param {number} num 数字
     * @returns 返回字符
     */
    $.fillZero = function (num) {
        num = parseInt(num);
        if (num < 10) {
            return "0" + num;
        } else {
            return "" + num;
        }
    };

    /**
     * 获取随机数,默认是 0-10
     * @param {number} minValue 随机数最小值
     * @param {number} maxValue 随机数最大值
     * @param {boolean} includeMaxValue 是否包含最大值,默认false
     */
    $.getRandom = function (minValue, maxValue, includeMaxValue) {
        if (minValue === undefined) {
            minValue = 0;
        }
        if (maxValue === undefined) {
            maxValue = 10;
        }
        if (includeMaxValue === undefined) {
            includeMaxValue = false;
        }
        var exp1;
        if (includeMaxValue) {
            exp1 = (maxValue - minValue + 1);
        } else {
            exp1 = (maxValue - minValue);
        }

        var retObj = {
            next: function () {
                return Math.floor(Math.random() * exp1) + minValue;
            }
        };
        return retObj;
    }

    /**
     * 生成一个 uuid/guid
     */
    $.uuid = $.uuidv4 = function () {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }

    /**
     * 生成一个 uuid/guid,es6的版本
     */
    $.uuides6 = $.uuidv4es6 = function () {
        return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
            (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        );
    }
    /**
     * 生成验证码
     * @param length 验证码长度，默认 6
     */
    $.genValidateCode = function (length) {
        if (length === undefined) {
            length = 6;
        }
        //去除 数字0 1 小写字母 i l o 大写字母 I O 这些字符容易混淆
        var text = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
        var code = "";
        for (var i = 0; i < length; i++) {
            var index = Math.floor(Math.random() * text.length);//不包含长度最大值
            var c = text[index];
            code += c;
        }
        return code;
    }

    return $;
});